package hu.veilingsite.client;

import hu.veilingsite.gui.Categories;
import hu.veilingsite.gui.CreditsKopen;
import hu.veilingsite.gui.EditAuction;
import hu.veilingsite.gui.EditProfile;
import hu.veilingsite.gui.Inloggen;
import hu.veilingsite.gui.Profile;
import hu.veilingsite.gui.ProfileAdminAuctions;
import hu.veilingsite.gui.ProfileAdminUsers;
import hu.veilingsite.gui.ProfileAuctions;
import hu.veilingsite.gui.ProfileBids;
import hu.veilingsite.gui.Registreeren;
import hu.veilingsite.gui.ShowAuctions;
import hu.veilingsite.gui.Statistics;
import hu.veilingsite.gui.ViewAuction;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;

public class HistoryListener {

	public HistoryListener() {
		String initToken = History.getToken();
		if(initToken.length() == 0) {
			History.newItem("home");
		} else {
			historyChanged(initToken);
		}
		TheListener();
	}
	/*
	 * HistoryListener checkt de link (#foo) en wanneer dit een bekende pagina is, laad hij deze.
	 */
	private void TheListener() {
		History.addValueChangeHandler(new ValueChangeHandler<String>() {
			@Override
			public void onValueChange(ValueChangeEvent<String> event) {
				String token = History.getToken();
				historyChanged(token);				
			}
		});
	}

	private void historyChanged(String token) {
		RootPanel main = RootPanel.get("main");
		main.clear();
		if(token.equals("login")) {
			main.add(new Inloggen().createPage());
		} else if(token.equals("home")) {
			main.add(new Veilingsite().createHome());
		} else if(token.equals("registreer")) {
			main.add(new Registreeren().createPage());
		} else if(token.equals("veilingen")) {
			main.add(new ShowAuctions().createPage());
		} else if(token.equals("veiling&2")) {
			main.add(new ViewAuction(2).createPage());
		} else if(token.equals("categories")) {
			main.add(new Categories().createPage());
		} else if(token.equals("statistics")) {
			main.add(new Statistics().createPage());
		} else if(token.equals("profile")) {
			main.add(new Profile().createPage());
		} else if(token.equals("editProfile")) {
			main.add(new EditProfile().createPage());
		} else if(token.equals("profileAuctions")) {
			main.add(new ProfileAuctions().createPage());
		} else if(token.equals("credits")) {
			main.add(new CreditsKopen().createPage());
		} else if(token.equals("profileBids")) {
			main.add(new ProfileBids().createPage());
		} else if(token.equals("profileAdminAuctions")) {
			if(Systeem.ingelogd.isIsadmin()){
				main.add(new ProfileAdminAuctions().createPage());
			}
			else{
				Window.alert("u moet admin zijn om hier te kunnen komen");
				main.add(new Veilingsite().createHome());
			}
		} else if(token.equals("profileAdminUsers")) {
			if(Systeem.ingelogd.isIsadmin()){
				new ProfileAdminUsers().createPage();
			}
			else{
				Window.alert("u moet admin zijn om hier te kunnen komen");
				main.add(new Veilingsite().createHome());
			}
		} else if(token.equals("logout")){
			Systeem.uitloggen();
			main.add(new Veilingsite().createHome());
		} else if(token.length() >= 16){
			if(token.substring(0, 15).equals("wijzig_veiling&")) {
				String s = token.substring(15);
				new EditAuction().createPage(Integer.parseInt(s));
			}
			else{
				main.add(new Veilingsite().createHome());
			}
		} else {
			main.add(new Veilingsite().createHome());
		}
	}
}